package cn.hloger.spider.demo.authDemo.handler;

import cn.hloger.spider.demo.authDemo.utils.AESUtil;
import cn.hloger.spider.demo.authDemo.utils.SignUtils;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * 签名认证拦截器
 */
@Component
public class ParamDecodeHandler {
    /**
     * aes密钥
     */
    @Value("${secretkey:null}")
    private String secretkey;

    public String decode(Map<String, String> map) {
        if (CollectionUtils.isEmpty(map)) {
            throw new IllegalArgumentException("参数错误,参数不能为空");
        }
        String bizContent = map.get("biz_content");
        if (StringUtils.isEmpty(map)) {
            throw new IllegalArgumentException("参数错误,content不能为空");
        }
        return AESUtil.aesDecry(bizContent, secretkey);
    }


    public Map<String, String> decodeForMap(Map<String, String> map) {
        return JSONObject.parseObject(decode(map), Map.class);
    }

    /**
     * 验签工具
     */
    public static void main(String[] args) {
//        String str = "lfyh00001";
//        String str = "{\"cxlx\":\"1\",\"name_man\":\"党院世\",\"cert_num_man\":\"00169\",\"name_woman\":\"程兴陶\",\"cert_num_woman\":\"132302198104201824\",\"sqztxh\":\"\",\"yhbm\":\"\"}";
//        String str = "{\"cxlx\":\"1\",\"name_man\":\"党院世\",\"cert_num_man\":\"00169\",\"name_woman\":\"程兴陶\",\"cert_num_woman\":\"132302198104201824\",\"sqztxh\":\"\",\"yhbm\":\"\"}";
        String str = "{\"cxlx\":\"2\",\"name_man\":\"党院世\",\"cert_num_man\":\"00169\",\"name_woman\":\"程兴陶\",\"cert_num_woman\":\"132302198104201824\",\"sqztxh\":\"\",\"yhbm\":\"\"}";
        String base = "PmxAAq4bIi1EwxWE5+Dzaw==";
        String mw = AESUtil.aesEncry(str, base);
        System.out.println(mw);
        String mww = AESUtil.aesDecry(mw, base);
        System.out.println(mww);
        String time = "1711366465000";
        String sign = SignUtils.toSign(mw, "333", time);
//        String sign2 = SignUtils.toSign(
//                 "YFH3iC7j8QJSjwgclSmb1U7Pg0qjTeakgvnI69oQoMPqbhDekTMDVn7WvWMaEG0usfZeMbaM5WBUhz//clOFs43azfHN TymW5tJCNVWt54rncp1CqitjDExOJ8n6o78d9oHIW4U/2NL0a0rzs2RtIhAVyJqa4e7R3Ap6Uwn36KAZEaQgaxQGLYWRp/lSRkk",
//                "333",
//                "1711366465000");
        System.out.println("sign:" + sign);
    }

    //    public static void main(String[] args) throws NoSuchAlgorithmException {
//    生成AES 的key
//            KeyGenerator keyGen = KeyGenerator.getInstance("AES");
//            keyGen.init(128); // for example
//            SecretKey secretKey = keyGen.generateKey();
//            String encodedKey = Base64.getEncoder().encodeToString(secretKey.getEncoded());
//            System.out.println("AES Secret Key: " + encodedKey);
////
//    }


}

